﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using YuuzleComic.Common;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO.Compression;

public partial class yzadmin_data_collect_jmydm : YuuzleComic.Web.UI.ManagePage
{
    protected string json = "";
    private JObject result = new JObject();
    private static CookieContainer cookies = new CookieContainer();
    private static string online = "";
    private static System.Timers.Timer myTimer;
    string domain = "http://www.jmydm.com";
    string baseUrl = "http://www.jmydm.com/{0}-{1}/";
    string searchUrl = "http://www.jmydm.com/comic/s.aspx?x=u&tbSTxt={0}";
    string type = "ComicList";
    int p = 1;
    string xgd = "0";
    private string getUrl()
    {
        return string.Format(baseUrl, new object[] { type, p });
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request["a"])
            {
                case "list":
                    this.ProcessList();
                    break;
                case "single":
                    this.ProcessSingle();
                    break;
                case "import":
                    this.ProcessImport();
                    break;
                case "validate":
                    this.ProcessValidate();
                    break;
                default:
                    break;
            }
        }
    }

    private void ProcessValidate()
    {
        try
        {
            Response.Clear();
            Response.ContentType = "image/jpeg";
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create("http://www.jmydm.com/login/validate.aspx?r=" + Request["r"]);
            myHttpWebRequest.CookieContainer = cookies;
            myHttpWebRequest.KeepAlive = true;
            WebResponse response = myHttpWebRequest.GetResponse();
            Stream st = response.GetResponseStream();
            long length = response.ContentLength;
            byte[] by = new byte[length];
            st.Read(by, 0, (int)length);
            Response.BinaryWrite(by);
            response.Close();
            Response.End();
        }
        catch (Exception)
        {
            
            throw;
        }
    }

    private void ProcessImport()
    {
        try
        {
            HtmlWeb webClient = this.getWebClient();
            string[] urlArray = Request["zjurl"].Split('\n');
            Array.Reverse(urlArray);//数组倒序
            foreach (string url in urlArray)
            {
                List<string> tpurl = new List<string>();
                HtmlDocument doc = doc = webClient.Load(url);

                YuuzleComic.Model.zj zj = new YuuzleComic.Model.zj();
                zj.cid = Convert.ToInt32(Request["id"]);
                if (doc.GetElementbyId("hdPics") == null)
                {
                    string[] scriptArr = doc.DocumentNode.SelectSingleNode("/html/head/script[1]").InnerText.Split(new string[] { "var sFiles=\"", "\";var sPath=\"", "\";" }, StringSplitOptions.None);
                    foreach (string item in scriptArr[1].Split('|'))
                    {
                        tpurl.Add(scriptArr[2] + item);
                    }
                    zj.zjmc = doc.DocumentNode.SelectSingleNode("//span[@id=\"spt2\"]").InnerText.Replace(doc.DocumentNode.SelectSingleNode("//span[@id=\"spt1\"]").InnerText, "").Trim();
                }
                else
                {
                    string[] hdPics = doc.GetElementbyId("hdPics").Attributes["value"].Value.Split('|');
                    foreach (string item in hdPics)
                    {
                        tpurl.Add(doc.GetElementbyId("hdPath").Attributes["value"].Value + item);
                    }
                    zj.zjmc = doc.GetElementbyId("hdTitle").Attributes["value"].Value.Split(new string[] { " - " }, StringSplitOptions.None)[1];
;
                }
                
                zj.time = DateTime.Now;
                zj.isdownload = false;
                zj.tpadd = Request["tpadd"] + zj.zjmc.Split(' ')[0] + "/";
                zj.tps = tpurl.Count;
                zj.tpurl = string.Join("|", tpurl.ToArray());
                if (zj.zjmc.Contains("Vol_"))
                {
                    zj.zjmc = "第" + zj.zjmc.Replace("Vol_", "") + "卷";
                }
                YuuzleComic.BLL.zj zjbll = new YuuzleComic.BLL.zj();
                if (urlArray.Length == 1 && !string.IsNullOrEmpty(Request["zjid"]))
                {
                    zj.id = int.Parse(Request["zjid"]);
                    zjbll.Update(zj);
                }
                else
                {
                    zjbll.Add(zj);
                }
                if (doc.GetElementbyId("hdPics") != null)
	            {
                    string posturl = "http://www.jmydm.com/comic/v.aspx";
                    string postdata = "hdPics=" + doc.GetElementbyId("hdPics").Attributes["value"].Value + "&hdDomain=" + doc.GetElementbyId("hdDomain").Attributes["value"].Value + "&hdTitle=" + doc.GetElementbyId("hdTitle").Attributes["value"].Value + "&hdInfoID=" + doc.GetElementbyId("hdInfoID").Attributes["value"].Value + "&hdPath=" + doc.GetElementbyId("hdPath").Attributes["value"].Value + "&hdCuDomain=" + (doc.GetElementbyId("hdCuDomain").Attributes["value"] != null ? doc.GetElementbyId("hdCuDomain").Attributes["value"].Value : "");
                    string postreferer = url;
                    this.post(posturl, postreferer, postdata);
	            }
            }

            result.Add("success", true);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally
        {
            json = result.ToString();
        }
    }

    

    private void ProcessSingle()
    {
        try
        {
            HtmlWeb webClient = this.getWebClient();
            HtmlDocument doc = webClient.Load(Request["url"]);
            HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='cComicText']");
            result.Add("mhmc", node.SelectSingleNode("div[2]/table/tr[1]/td[2]").InnerText.Trim());
            result.Add("mhfm", node.SelectSingleNode("div[1]/img").Attributes["src"].Value);
            List<string> zzmclist = new List<string>();
            foreach (HtmlNode item in node.SelectNodes("div[2]/table/tr[3]/td[2]/a[position()>1]"))
            {
                zzmclist.Add(item.InnerText.Trim());
            }
            result.Add("zzmc", string.Join(",", zzmclist.ToArray()));
            result.Add("diqu", node.SelectSingleNode("div[2]/table/tr[3]/td[2]/a[1]").InnerText);
            result.Add("classname", doc.DocumentNode.SelectSingleNode("//div[@class='cBadge']/a[2]").InnerText);
            result.Add("zt", node.SelectSingleNode("div[2]/table/tr[2]/td[2]/span").InnerText.Trim().Split(' ')[0]);
            result.Add("tc", node.SelectSingleNode("div[2]/table/tr[4]/td[2]").InnerText.Trim());
            result.Add("time", node.SelectSingleNode("div[2]/table/tr[10]/td[2]/span").InnerText.Split('：')[1]);
            result.Add("mhjj", doc.DocumentNode.SelectSingleNode("//div[@class='cInfoArea']").InnerText.Trim());
            result.Add("mhjs", doc.DocumentNode.SelectSingleNode("//div[@class='cInfoArea']").InnerHtml.Trim());
            JArray chapters = new JArray();
            HtmlNodeCollection tagNodes = doc.DocumentNode.SelectNodes("//div[@class='cVolTag']");
            //HtmlNodeCollection volNodes = doc.DocumentNode.SelectNodes("//ul[@id=\"cUlVolList\"]");
            if (tagNodes != null)
            {
                for (int i = 0; i < tagNodes.Count; i++)
                {
                    HtmlNodeCollection volNodes = tagNodes[i].NextSibling.SelectNodes("li/a");
                    foreach (HtmlNode item in volNodes)
                    {
                        JObject chapter = new JObject();
                        chapter.Add("zjmc", item.InnerText);
                        chapter.Add("zjurl", domain + item.Attributes["href"].Value);
                        chapter.Add("type", tagNodes[i].InnerText);
                        chapters.Add(chapter);
                    }
                }
            }
            result.Add("chapters", chapters);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message);
        }
        finally {
            json = result.ToString();
        }
    }

    private void ProcessList()
    {
        try
        {
            p = Convert.ToInt32(Request["page"]);
            JArray data = new JArray();
            HtmlDocument doc;
            HtmlNodeCollection nodes;
            int total;
            if (string.IsNullOrEmpty(Request["query"]))
            {
                if (!string.IsNullOrEmpty(Request["type"]))
                {
                    type = Request["type"];
                }
                doc = this.getWebClient().Load(this.getUrl());
                nodes = doc.DocumentNode.SelectNodes("//dl[@class='cComicDL']/div");
                if (nodes != null)
                {
                    foreach (HtmlNode node in nodes)
                    {
                        JObject jo = new JObject();
                        string[] info = node.SelectSingleNode("dt/a").Attributes["title"].Value.Split(new string[] { " - " }, StringSplitOptions.None);
                        jo.Add("mhmc", info[0]);
                        jo.Add("mhurl", domain + node.SelectSingleNode("dt/a").Attributes["href"].Value);
                        jo.Add("zzmc", info[1]);
                        jo.Add("zjmc", node.SelectSingleNode("dt/a/span").InnerText);
                        jo.Add("time", info[2]);
                        data.Add(jo);
                    }
                }
                total = int.Parse(doc.DocumentNode.SelectSingleNode("//*[@id=\"iComicPC1\"]/b[1]").InnerText);
            }else
            {
                doc = this.getWebClient().Load(string.Format(searchUrl, new object[] { Request["query"].Trim() }));
                if (doc.GetElementbyId("Publogin1_txtUserID") != null)
                {
                    string posturl = "http://www.jmydm.com/login/userlogin.aspx";
                    string postreferer = "http://www.jmydm.com/login/userlogin.aspx";
                    string postdata = "__VIEWSTATE=" + Uri.EscapeDataString(doc.GetElementbyId("__VIEWSTATE").Attributes["value"].Value) + "&__EVENTVALIDATION=" + Uri.EscapeDataString(doc.GetElementbyId("__EVENTVALIDATION").Attributes["value"].Value) + "&Publogin1:txtUserID=seo1214&Publogin1:txtPsd=5201314&Publogin1:txtValida=" + Request["code"].Trim() + "&Publogin1:btnLogin=登 陆";
                    doc.LoadHtml(this.post(posturl, postreferer, postdata));
                    if (doc.GetElementbyId("Publogin1_lblMsg") != null)
                    {
                        result.Add("success", false);
                        result.Add("errorMessage", doc.GetElementbyId("Publogin1_lblMsg").InnerText);
                        return;
                    }
                }
                nodes = doc.DocumentNode.SelectNodes("//div[@class=\"cInfoItem\"]");
                if (nodes != null)
                {
                    foreach (HtmlNode node in nodes)
                    {
                        JObject jo = new JObject();
                        HtmlNodeCollection namenodes = node.SelectNodes("div[2]/a/text() | div[2]/a/font");
                        string mhmc = "";
                        foreach (HtmlNode name in namenodes)
                        {
                            mhmc += name.InnerText;
                        }
                        jo.Add("mhmc", mhmc.Trim());
                        jo.Add("mhurl", domain + node.SelectSingleNode("div[2]/a").Attributes["href"].Value);
                        jo.Add("zzmc", node.SelectSingleNode("div[3]/span[2]/a").InnerText);
                        jo.Add("zjmc", node.SelectSingleNode("div[2]/a/span").InnerText);
                        jo.Add("time", node.SelectSingleNode("div[4]/span").InnerText.Split(new string[] { " 于 " }, StringSplitOptions.None)[1]);
                        data.Add(jo);
                    }
                }
                total = int.Parse(doc.DocumentNode.SelectSingleNode("//div[@class='cAreaTitle']/font").InnerText);
	        }
            
            result.Add("data", data);
            result.Add("total", total);
            result.Add("online", online);
        }
        catch (Exception e)
        {
            result.Add("success", false);
            result.Add("errorMessage", e.Message + e.StackTrace);
        }
        finally {
            json = result.ToString();
        }

    }

    private HtmlWeb getWebClient()
    {
        HtmlWeb webClient = new HtmlWeb();
        webClient.UseCookies = true;
        HtmlAgilityPack.HtmlWeb.PreRequestHandler handler = delegate(HttpWebRequest request)
        {
            request.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            request.CookieContainer = cookies;
            request.Timeout = 500000;
            return true;
        };
        webClient.PreRequest += handler;
        return webClient;
    }

    private string post(string url, string Referer, string postdata)
    {
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create(url);
        myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        myHttpWebRequest.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en;q=0.6";
        myHttpWebRequest.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
        myHttpWebRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate,sdch";
        myHttpWebRequest.Headers[HttpRequestHeader.CacheControl] = "max-age=0";
        myHttpWebRequest.Headers["Origin"] = "http://www.jmydm.com";
        myHttpWebRequest.Referer = Referer;
        myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31";
        myHttpWebRequest.KeepAlive = true;
        myHttpWebRequest.CookieContainer = cookies;
        //上面的http头看情况而定，但是下面俩必须加  
        myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
        myHttpWebRequest.Method = "POST";
        #region 填充要post的内容
        byte[] postBytes = Encoding.UTF8.GetBytes(postdata);
        myHttpWebRequest.ContentLength = postBytes.Length;
        System.IO.Stream requestStream = myHttpWebRequest.GetRequestStream();
        requestStream.Write(postBytes, 0, postBytes.Length);
        requestStream.Close();
        #endregion
        WebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse();
        Stream responseStream = response.GetResponseStream();
        //如果http头中接受gzip的话，这里就要判断是否为有压缩，有的话，直接解压缩即可  
        if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
        {
            responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
        }
        string stringResponse;
        using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream))
        {
            stringResponse = responseReader.ReadToEnd();
        }
        response.Close();
        this.keepLogon();
        return stringResponse;
    }

    private void keepLogon(){ 
    //在应用程序启动时运行的代码
        if (myTimer == null)
        {
            myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            myTimer.Interval = 250000;
            myTimer.Enabled = true;
        }
    }

    private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)System.Net.WebRequest.Create("http://www.jmydm.com/user/");
            myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            myHttpWebRequest.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en;q=0.6";
            myHttpWebRequest.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
            myHttpWebRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip,deflate,sdch";
            myHttpWebRequest.Headers[HttpRequestHeader.CacheControl] = "max-age=0";
            myHttpWebRequest.Referer = "http://www.jmydm.com/user/";
            myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31";
            myHttpWebRequest.KeepAlive = true;
            myHttpWebRequest.CookieContainer = cookies;
            myHttpWebRequest.Method = "GET";
            WebResponse response = myHttpWebRequest.GetResponse();
            HtmlDocument doc = new HtmlDocument();
            Stream responseStream = response.GetResponseStream();
            //如果http头中接受gzip的话，这里就要判断是否为有压缩，有的话，直接解压缩即可  
            if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
            {
                responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
            }
            using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream))
            {
                doc.LoadHtml(responseReader.ReadToEnd());
            }
            online = doc.DocumentNode.SelectSingleNode("//div[@class='cUDatat2']/b[1]").InnerText.Trim() + "<br>" +
                doc.DocumentNode.SelectSingleNode("//div[@class='cUDatat2']/b[2]").InnerText.Trim() + "<br>" +
               doc.GetElementbyId("spTimePay").InnerText.Trim();
            response.Close();
        }
        catch (Exception)
        {
            
        }

    }
}